'''# Third_party'''
from openpyxl.styles import *

'''# built_in'''
import traceback

'''custom'''
from action.key_Retrieval import SurveyKeyWord
from action.page_Action import KeyWordTest
from dataRead.auxiliary.cell_Value_Handle import CellValueHandle
# from utils.decorator import case_retry
from utils.log.trusteeship import trusteeship


class RunSheetRow(CellValueHandle):

    def __repr__(self) -> str:
        return '单条步骤行操作处理'

    def __init__(self) -> None:
        super().__init__()
        self.survey = SurveyKeyWord()
        self.keyWord = KeyWordTest()
        self.AbnormalFont = Font(size=11, bold=True, italic=True, color="ae8abe")  # 异常字体样式
        self.FailFont = Font(size=11, bold=True, color="ff0000")  # 未通过字体样式
        self.PassFont = Font(size=11, bold=True, color="00b050")  # 通过字体样式

    # @case_retry
    def __call__(self, WB_Object, WS_Object, ws_rowsMax, elementExpression, writeColumn, caseStepsFile_Path, acceptResult):
        """
         参数：
            @param WB_Object : 模块用例步骤文件对象
            @param WS_Object : 模块用例步骤文件单独sheet表对象
            @param ws_Rows_Max : sheet表最大操作行数
            @param elementExpression : 模块用例caseStep.yaml文件内容
            @param writeColumn : 主题测试结果单元格书写列号
            @param caseStepsFile_Path : 模块用例步骤文件路径;
            @param acceptResult : 断言结果暂存数组
        """
        result_Step_Assert = True  # 步骤行执行结果“断言判断开关”
        result_Step_Abnormal = False  # 步骤行执行结果“异常判断开关”
        start_Row = 2  # sheet表，“行”起始位置
        case_Error = ''  # 步骤异常信息

        while start_Row <= ws_rowsMax:
            cellValueAggregation = super().__call__(WS_Object, start_Row, elementExpression)

            WS_Object.cell(row=start_Row, column=7).value = cellValueAggregation['executionTime']  # 写入时间
            WS_Object.cell(row=start_Row, column=writeColumn).font = self.PassFont
            WS_Object.cell(row=start_Row, column=writeColumn).value = 'Pass'                  # 预写入Pass

            try:
                '''调用关键字检索函数, 完成相应操作'''
                result_Step_Assert = self.survey.keyRetrieval(self.keyWord, 
                    cellValueAggregation['tagging'], 
                    cellValueAggregation['keywordName'], 
                    cellValueAggregation['positionInformation'], 
                    cellValueAggregation['manipulatedValue'])
            except Exception:
                '''异常抛出，写入Excel文件中'''
                trusteeship.exceptinfo("\nAn exception problem was caught: ")
                result_Step_Abnormal = True  # 异常开关打开
                case_Error = traceback.format_exc()  # 异常信息
                WS_Object.cell(row=start_Row, column=writeColumn).font = self.AbnormalFont
                WS_Object.cell(row=start_Row, column=writeColumn).value = '异常'               # 写入异常
                WS_Object.cell(row=start_Row, column=writeColumn+1).value = case_Error
            else:
                '''未出现异常，断言结果判断'''
                if (not result_Step_Assert) and result_Step_Assert != None:
                    WS_Object.cell(row=start_Row, column=writeColumn).font = self.FailFont
                    WS_Object.cell(row=start_Row, column=writeColumn).value = 'Fail'           # 写入Fail
                    acceptResult.append(False)

            start_Row += 1  # 步骤行值变化

        WB_Object.save(caseStepsFile_Path)
        return {'abnormalJudgment': result_Step_Abnormal, 'caseError': case_Error, 'acceptResult': acceptResult}